/*
 * Created by JFormDesigner on Thu Jun 27 08:12:54 CST 2024
 */

package scct.hanyuesheng.ui;

import java.awt.event.*;
import scct.hanyuesheng.dao.UserDao;
import scct.hanyuesheng.model.User;

import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelUserManger extends JPanel {
    DefaultTableModel model;
    FormMain main;
    public PanelUserManger(FormMain main) {
        this.main=main;
        initComponents();
        initView();//定义初始化方法
        

    }


    private void initView(){
//        自己初始化
//        获取表格对象的数据模型，通过对模型的操作，为表格添加和实现数据
        model = (DefaultTableModel) tabUser.getModel();//alt+enter
        model.addColumn("id");
        model.addColumn("用户名");
        model.addColumn("密码");
        model.addColumn("性别");
        model.addColumn("手机号码");
        model.addColumn("年龄");
        getTable();

    }
    private void getTable() {

        model.setRowCount(0);
        UserDao userDao=new UserDao();
        List<User> allUser=userDao.getAllUser();
        for(User user:allUser){

            model.addRow(new Object[]  {user.getId(), user.getUsername(),
                    user.getPassword(), user.getGender(), user.getMobile(),user.getAge()});

        }

    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
    DlgUserAdd dlgUserAdd=new DlgUserAdd(main);
//    设置为模态对话框
    dlgUserAdd.setModal(true);
    dlgUserAdd.setVisible(true);

    getTable();
    }

    private void btnupdate(ActionEvent e) {
        // TODO add your code here
//        判断用户是否选中表中的一行
        if (tabUser.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择修改对象");
            return;
        }
        int index=tabUser.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgUserUpdate dlgUserUpdate = new DlgUserUpdate(main, id);
        dlgUserUpdate.setModal(true);
        dlgUserUpdate.setVisible(true);
        getTable();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        panel2 = new JPanel();
        btnAdd = new JButton();
        btnupdate = new JButton();
        scrollPane2 = new JScrollPane();
        tabUser = new JTable();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(
        0,0,0,0), "JF\u006frmDes\u0069gner \u0045valua\u0074ion",javax.swing.border.TitledBorder.CENTER,javax.swing.border.TitledBorder
        .BOTTOM,new java.awt.Font("D\u0069alog",java.awt.Font.BOLD,12),java.awt.Color.
        red), getBorder())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){@Override public void propertyChange(java.
        beans.PropertyChangeEvent e){if("\u0062order".equals(e.getPropertyName()))throw new RuntimeException();}});
        setLayout(new BorderLayout());

        //======== panel2 ========
        {
            panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel2.add(btnAdd);

            //---- btnupdate ----
            btnupdate.setText("\u4fee\u6539");
            btnupdate.addActionListener(e -> btnupdate(e));
            panel2.add(btnupdate);
        }
        add(panel2, BorderLayout.NORTH);

        //======== scrollPane2 ========
        {

            //---- tabUser ----
            tabUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollPane2.setViewportView(tabUser);
        }
        add(scrollPane2, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JPanel panel2;
    private JButton btnAdd;
    private JButton btnupdate;
    private JScrollPane scrollPane2;
    private JTable tabUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
